#include "usart4.h"


//#define USART2_TX_BUFFER_SIZE 0
//#define USART2_RX_BUFFER_SIZE 20U

void dma_configuration(void);
u8 Usart4_RXBUF[UART4_BUFLEN];


/*******************************??**********************************/





void Uart4_Init(uint32_t bound)
{
	gpio_init_type gpio_init_struct;
	dma_init_type dma_init_struct;

	crm_periph_clock_enable(CRM_UART4_PERIPH_CLOCK, TRUE);   //??USART1???
	crm_periph_clock_enable(UART4_GPIO_CRM_CLK, TRUE);

	gpio_default_para_init(&gpio_init_struct);

	/* configure the usart4 tx pin */
	gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
	gpio_init_struct.gpio_out_type  = GPIO_OUTPUT_PUSH_PULL;
	gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
	gpio_init_struct.gpio_pins = UART4_TX_PIN|UART4_RX_PIN;
	gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
	gpio_init(UART4_GPIO, &gpio_init_struct);

	gpio_pin_mux_config(UART4_GPIO, GPIO_PINS_SOURCE0, UART4_PIN_MUX_NUM);//??IO?????
	gpio_pin_mux_config(UART4_GPIO, GPIO_PINS_SOURCE1, UART4_PIN_MUX_NUM);//??IO?????


	//DMA????
	crm_periph_clock_enable(CRM_DMA1_PERIPH_CLOCK, TRUE);
	dmamux_enable(DMA1, TRUE);//????? DMAMUX?
	dma_reset(DMA1_CHANNEL3);
	dma_default_para_init(&dma_init_struct);
	dma_init_struct.buffer_size = UART4_BUFLEN;                     					//????
	dma_init_struct.direction = DMA_DIR_PERIPHERAL_TO_MEMORY;                	//?????????
	dma_init_struct.memory_base_addr = (uint32_t)&Usart4_RXBUF;               //????
	dma_init_struct.memory_data_width = DMA_MEMORY_DATA_WIDTH_BYTE;          	//??????? 8bit
	dma_init_struct.memory_inc_enable = TRUE;                                	//????????
	dma_init_struct.peripheral_base_addr = (uint32_t)&UART4->dt;            	//????
	dma_init_struct.peripheral_data_width = DMA_PERIPHERAL_DATA_WIDTH_BYTE;  	//??????? 8bit
	dma_init_struct.peripheral_inc_enable = FALSE;                           	//????????
	dma_init_struct.priority = DMA_PRIORITY_MEDIUM;                          	//?????
	dma_init_struct.loop_mode_enable = TRUE;                                	//??
	dma_init(DMA1_CHANNEL3, &dma_init_struct);

	dma_flexible_config(DMA1,DMA1MUX_CHANNEL3,DMAMUX_DMAREQ_ID_UART4_RX);  //DMA1???2?????2?DMA????
	usart_dma_receiver_enable(UART4, TRUE);                           //??DMA??
	dma_channel_enable(DMA1_CHANNEL3, TRUE); 													// usart7 rx ?? DMA ?? 

	
	
	//USART7??
	nvic_irq_enable(UART4_IRQn, usart4_preempt_priority, usart4_sub_priority);                               	//????1??,???0,????0
	/* configure usart4 param */
	usart_init(UART4, bound, USART_DATA_8BITS, USART_STOP_1_BIT);
	usart_transmitter_enable(UART4, TRUE);
	usart_receiver_enable(UART4, TRUE);
	usart_interrupt_enable(UART4, USART_IDLE_INT, TRUE);
	usart_enable(UART4, TRUE);
	usart_flag_clear(UART4,USART_IDLE_INT);

}


void UART4_IRQHandler(void)
{
	 uint8_t clear;
	
	 BaseType_t xHigherPriorityTaskWoken = pdFALSE;
	
	 if(usart_flag_get(UART4, USART_IDLEF_FLAG) != RESET)                   // USART7????
		 {
			 usart_data_receive(UART4);  																				//clear idle flag;
			 dma_channel_enable(DMA1_CHANNEL3, FALSE);     											//??DMA??
			 dma_data_number_set(DMA1_CHANNEL3, UART4_BUFLEN);
			 //usart_flag_clear(UART4,USART_IDLE_INT);
			 xSemaphoreGiveFromISR(Usart4_sem,&xHigherPriorityTaskWoken);				//?????
		 }
}

void Uart4Send(uint8_t *buff,uint8_t size)
{
	uint8_t *Enter_click = buff;
	
	for (uint8_t uart4_tx_cnt = 0; uart4_tx_cnt < size; uart4_tx_cnt++)
	{
		while (usart_flag_get(UART4, USART_TDBE_FLAG) == RESET)
			;
		usart_data_transmit(UART4, Enter_click[uart4_tx_cnt]);
		while (usart_flag_get(UART4, USART_TDC_FLAG) == RESET)
			;
	}
}






